<template>
  <a-row
    type="flex"
    justify="space-between"
    align="middle"
    style="flex-wrap: nowrap"
  >
    <BSelect v-model="left" :list="leftDateList" @change2="handleCompare(1)" style="width: 45%;" />
    <span style="flex-shrink: 0">至</span>
    <BSelect v-model="right" :list="rightDateList" @change2="handleCompare(2)" style="width: 45%;" />
  </a-row>
</template>
<script>
import moment from 'moment'

export default {
  name: 'QuarterSelect',
  components: {},
  props: {
    value: [String, Array, Object],
    rangeFormat: [String, Array, Object]
  },
  data() {
    return {
      left: '',
      right: '',
      leftDateList: [],
      rightDateList: []
    }
  },
  computed: {},
  watch: {
    value(newVal) {
      if (!newVal) {
        this.left = null
        this.right = null
      }
    }
  },
  created() {
    let temp = []
    let currentYear = (new Date()).getFullYear()
    let curryMonth = (new Date()).getMonth()
    // let currentQuarter = ((new Date()).getMonth() + 1) % 3
    let currentQuarter = curryMonth? Math.floor(curryMonth/3):0;
    let rela = { 1: '一', 2: '二', 3: '三', 4: '四' }
    this.time = `${currentYear}年第${rela[currentQuarter + 1]}季度`
    Array.from({ length: ((currentYear - 2000) * 4 + currentQuarter + 1) }).map((v, i) => {
      let year = parseInt(i / 4)
      let quarter = i % 4

      if (year < 10) year = '0' + year
      let name = `20${year}年第${rela[quarter + 1]}季度`
      temp.push({ id: name, name, i })
    })
    temp.sort((a, b) => b.i - a.i)
    this.rightDateList = this.leftDateList = temp
  },
  methods: {
    handleCompare(type) {
      if (!this.left) return
      if (!this.right) return
      if (moment(this.left.split('/')[1]).isAfter(moment(this.right.split('/')[1]))) {
        // if (type === 1) this.left = null
        // if (type === 2) this.right = null
        return this.$message.warning('开始季度不能大于结束季度！')
      }
      let r = {}
      r[this.rangeFormat[0]] = this.left.split('/')[0]
      r[this.rangeFormat[1]] = this.right.split('/')[0]
      this.$emit('change', r)
      console.log(r)
    }
  }
}
</script>
